package com.example.helloworld;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class RunnableNHandler extends Activity {

	private ProgressBar bar;
	private TextView text;
	boolean isRunning =	false;
	Handler handler;
	ProgressRunnable runnable;
	
	private TextView textRequest;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_runnable_n_handler);
		
		bar =				(ProgressBar)findViewById(R.id.progress);
		text =				(TextView)findViewById(R.id.textStatus);
		
		handler =			new Handler();
		runnable =			new ProgressRunnable();
		
		textRequest =		(TextView)findViewById(R.id.textRequest);
		Button request =	(Button)findViewById(R.id.request);
		request.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				request();
			}
		});
	}
	
	private void request() {
		String title =			"Remote Request";
		String message =		"Request OK ?";
		String titleButtonYes =	"Yes";
		String titleButtonNo =	"No";
		
		AlertDialog.Builder builder =	
								new AlertDialog.Builder(this);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setPositiveButton(titleButtonYes, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				RequestHandler handler =	new RequestHandler();
				handler.sendEmptyMessageDelayed(0, 20);
			}
		});
		
		builder.setNegativeButton(titleButtonNo, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {}
		});
		
		AlertDialog dialog =	builder.show();	
		dialog.show();
		
		textRequest.setText("Request start");
	}
	
	class RequestHandler extends Handler {
		@Override
		public void handleMessage(Message message) {
			for (int k = 0; k < 10; k++) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					Log.d("Handler", "InterruptedException", e);
				}
			}
			textRequest.setText("Request end");
		}
	}
	
	@Override
	public void onStart() {
		super.onStart();
		
		bar.setProgress(0);
		
		Thread thread =	new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					for (int i = 0; i < 20 && isRunning; i++) {
						Thread.sleep(1000);
						
						handler.post(runnable);
					}
				} catch (Exception e) {
					Log.e(this.getClass().getName(), "Exception in processing message", e);
				}
			}
		});
		
		isRunning =	true;
		thread.start();
	}
	
	@Override
	public void onStop() {
		super.onStop();
		
		isRunning = false;
	}
	
	public class ProgressRunnable implements Runnable {
		@Override
		public void run() {
			bar.incrementProgressBy(5);
			
			if (bar.getProgress() == bar.getMax()) {
				text.setText("Runnable Done.");
			} else {
				text.setText("Runnable Working ... " + bar.getProgress());
			}
		}
	}

}
